package com.gjs.freechat.server.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import com.gjs.freechat.server.component.bean.UserInfo;
import com.gjs.freechat.server.dao.mapper.UserInfoMapper;

@Repository
public class UserDaoImpl implements UserDao {
	
	@Autowired
	UserInfoMapper userInfoMapper;
	
//	@Override
//	public boolean validateUser(String userKey, int way) {
//		return userInfoMapper.validateUser(userKey, way) != null;
//	}
	
	@Override
	public UserInfo getUserInfoByUserName(String username) {
		//System.out.println(username);
		return userInfoMapper.getUserInfo(GET_USER_WAY_BY_USERNAME, username);
	}
	
	@Override
	public UserInfo getUserInfoByUserNameOrEmail(String userKey) {
		return userInfoMapper.getUserInfo(GET_USER_WAY_BY_USERNAME_OR_EMAIL, userKey);
	}
	
	@Override
	public UserInfo getUserInfo(String userKey, String password) {
		return userInfoMapper.getUserInfo(UserDao.GET_USER_WAY_BY_PASSWORD, userKey, password);
	}
	
//	@Override
//	public String[] getAllAvatarIconName() {
//		return userInfoMapper.getAllAvatarIconName();
//	}
	
	@Override
	public boolean saveNewUser(UserInfo userInfo) {
		boolean success = true;
		try {
			userInfoMapper.saveNewUserInfo(userInfo);
			userInfoMapper.saveNewUserPassword(userInfo);
		} catch (DataAccessException e) {
			e.printStackTrace();
			if (e instanceof DataIntegrityViolationException) {
				success = false;
			}
		}
		return success;
		
	}
}
